import json
import logging
from datetime import datetime
import requests

from app.db.database import SessionLocal
from app.db.crud import create_tpost
from app.db.models import TPost
from app.pacong.content_fetcher import ContentFetcher

logger = logging.getLogger(__name__)

class ToutiaoHotListFetcher(ContentFetcher):
    """头条热榜爬虫"""

    def __init__(self):
        super().__init__()
        self.source = "toutiao"
        self.source_name = "头条热榜"
        self.content_type = 15  # 头条的source_id设为1

    def fetch_content(self) -> list:
        """获取头条热榜数据"""
        url = "https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc"
        try:
            headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
                "Accept": "application/json, text/plain, */*",
                "Referer": "https://www.toutiao.com/"
            }
            response = requests.get(url, headers=headers)
            response.raise_for_status()
            data = response.json()
            
            # 处理响应数据
            hot_list = []
            for item in data["data"]:
                hot_item = {
                    "title": item["Title"],
                    "url": f"https://www.toutiao.com/trending/{item['ClusterIdStr']}/",
                    "pub_date": datetime.now(),  # 添加发布时间
                    "content": "",  # 添加空内容
                    "short_content": "",  # 添加空的简短内容
                }
                hot_list.append(hot_item)
            
            return hot_list
            
        except Exception as e:
            logger.error(f"获取头条热榜数据失败: {str(e)}")
            return []


if __name__ == "__main__":
    # 测试代码
    fetcher = ToutiaoHotListFetcher()
    fetcher.fetch_and_save() 